package hibernate.method;

import hibernate.method.interfaces.GameMethodsInterface;
import hibernate.model.Game;
import hibernate.model.GamesLike;
import hibernate.model.GamesRate;

import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class GameMethods implements GameMethodsInterface{

	@Override
	public void addGame(SessionFactory sessionFactory, Session session,
			String name, String describe, int min_users,
			int max_users) {
		Game g = new Game();
		g.setName(name);
		g.setDescribe(describe);
		g.setAdded(AdditionalMethods.getCurrentDate());
		g.setUsers_min(min_users);
		g.setUsers_max(max_users);	
		session.save(g);
	}

	@Override
	public void delete(SessionFactory sessionFactory, Session session, Game g) {
		//g.setGame_stat(Constraint.TRASH);
		session.delete(g);
	}

	

	@Override
	public void edit_describe(SessionFactory sessionFactory, Session session,
			Game g, String describe) {
		g.setDescribe(describe);
		
	}

	@Override
	public void edit_min_users(SessionFactory sessionFactory, Session session,
			Game g, int users_min) {
		g.setUsers_min(users_min);
		
	}

	@Override
	public void edit_max_users(SessionFactory sessionFactory, Session session,
			Game g, int users_max) {
		g.setUsers_max(users_max);
		
	}


	@Override
	public Game idToGame(SessionFactory sessionFactory, Session session,
			int game_id) {
		Object object = session.load(Game.class, game_id);
		Game g = (Game) object;
		return g;
	}

	@Override
	public List<Game> listAllGames(SessionFactory sessionFactory,
			Session session) {
		Criteria cr = session.createCriteria(Game.class);
		@SuppressWarnings("unchecked")
		List<Game> ulist = cr.list();
		return ulist;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Integer nameToId(SessionFactory sessionFactory, Session session,
			String gameName) {
		Query query = session.createSQLQuery(
				"select id from games s where s.game = :gameName")
				.setParameter("gameName", gameName);
				List result = query.list();
				return (Integer) result.get(0);
	}

	@Override
	public List<GamesRate> listAllGamesRates(SessionFactory sessionFactory,
			Session session) {
		Criteria cr = session.createCriteria(GamesRate.class);
		cr.addOrder(Order.asc("user_id"));
		cr.addOrder(Order.asc("game_id"));
		@SuppressWarnings("unchecked")
		List<GamesRate> ulist = cr.list();
		return ulist;
	}

	@Override
	public void like(SessionFactory sessionFactory, Session session,
			int user_id, int game_id) {
		GamesLike gl = new GamesLike();
		gl.setGame_id(game_id);
		gl.setUser_id(user_id);
		session.save(gl);
		
	}

	@Override
	public void dislike(SessionFactory sessionFactory, Session session,
			GamesLike gl) {
		session.delete(gl);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GamesLike> checkIfLike(SessionFactory sessionFactory,
			Session session, int user_id, int game_id) {
	Criteria cr = session.createCriteria(GamesLike.class);
		
		Criterion user = Restrictions.eq("user_id", user_id);
		Criterion game = Restrictions.eq("game_id", game_id);
	
		LogicalExpression andExp = Restrictions.and(user, game);
		cr.add(andExp);
		return cr.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GamesLike> listAllLiked(SessionFactory sessionFactory,
			Session session, int user_id) {
	Criteria cr = session.createCriteria(GamesLike.class);
		
		Criterion user = Restrictions.eq("user_id", user_id);
	
		cr.add(user);
		return cr.list();
	}

	@Override
	public GamesLike idToGamesLike(SessionFactory sessionFactory,
			Session session, int liked_id) {
		Object object = session.load(GamesLike.class, liked_id);
		GamesLike msg = (GamesLike) object;
		return msg;
	}

	public static byte[] getGameAvatar (Connection conn, int game_id)
		       throws Exception, SQLException
		  {
		    String req = "" ;
		    Blob img ;
		    byte[] imgData = null ;
		    Statement stmt = conn.createStatement();
		    
		    // Query
		    req = "Select avatar From gamesavatars Where game_id = " + game_id ;
		    
		    ResultSet rset  = stmt.executeQuery ( req ); 
		    
		    while (rset.next ())
		    {    
		      img = rset.getBlob(1);
		      imgData = img.getBytes(1,(int)img.length());
		    }    
		    
		    rset.close();
		    stmt.close();
		    
		    return imgData ;
		  }


}
